package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.AccidentCase
import cn.exrick.xboot.base.entity.Area
import cn.exrick.xboot.base.entity.SupervisionUser
import cn.exrick.xboot.base.mapper.AccidentCaseMapper
import cn.exrick.xboot.base.mapper.AreaMapper
import cn.exrick.xboot.base.service.IAccidentCaseService
import cn.exrick.xboot.base.service.IAreaService
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.ISupervisionUserService
import cn.exrick.xboot.base.vo.AccidentCaseVo
import cn.exrick.xboot.core.common.constant.CommonConstant
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.RoleService
import cn.exrick.xboot.core.service.UserService
import cn.exrick.xboot.core.service.mybatis.IUserRoleService
import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import reactor.core.publisher.Flux
import reactor.core.scheduler.Schedulers
import java.util.*
import javax.annotation.Resource

/**
 * 事故案例接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IAccidentCaseServiceImpl : ServiceImpl<AccidentCaseMapper?, AccidentCase?>(), IAccidentCaseService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iUserRoleService: IUserRoleService

    @Autowired
    private lateinit var roleService: RoleService

    @Autowired
    private lateinit var userService: UserService


    override fun createOrUpdateAccidentCase(accidentCase: AccidentCase): Result<Any?>? {

        return try {

            if (!iUserRoleService.findByUserId(securityUtil.currUser.id)?.map { it.id }?.contains(roleService.getRoleIdByRoleName(CommonConstant.ROLE_SECURITY))!!) {

                accidentCase.type = 2
            }

            accidentCase.areaId = securityUtil.currUser.id
            saveOrUpdate(accidentCase)
            ResultUtil.success("操作成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeAccidentCase(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("移除成功")
        }catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getAccidentCaseList(pageVo: PageVo, type: Int ): Result<MutableMap<String, Any?>>? {

        val accidentWrapper = KtQueryWrapper(AccidentCase::class.java).orderByDesc(AccidentCase::createTime)

        when (type) {

             1-> {

                 accidentWrapper.eq(AccidentCase::createBy, securityUtil.currUser.id)
                         .eq(AccidentCase::type, 1)
            }
            else -> {

                accidentWrapper.eq(AccidentCase::areaId, securityUtil.currUser.areaId)
                        .eq(AccidentCase::type, 2)
            }
        }

        return ToolUtil.setListToNul(list(accidentWrapper))?.run {

            val result:MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { AccidentCaseVo().apply { ToolUtil.copyProperties(it, this)
            } }.toMutableList())

            ResultUtil.data(result, "获取数据成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun getAccidentCaseDetail(id: String?): Result<AccidentCaseVo?>? {

        return getById(id)?.run {

            ResultUtil.data(AccidentCaseVo().apply {

                ToolUtil.copyProperties(this@run, this)
                createName = userService.get(createBy)?.nickname
                picsList = pics?.split(",")
                accidentSituation = enterpriseName.plus("在").plus(time).plus("发生").plus(accidentCaseType).plus("类型的事故,")
                        .plus("事故发生地点是:").plus(site).plus(",发生时间是:").plus(time).plus("死亡人数:").plus(deathToll).plus("受伤人数是:")
                        .plus(injuryNum)
            })
        } ?: ResultUtil.error(201, "不存在此事故案例")
    }

}